package com.itcanteen.reggie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcanteen.reggie.common.R;
import com.itcanteen.reggie.entity.Employee;
import com.itcanteen.reggie.exception.NameExistsException;
import com.itcanteen.reggie.mapper.EmployeeDao;
import com.itcanteen.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2022/10/22 10:08
 */

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired(required = false)
    EmployeeDao employeeDao;
    @Override
    public R<Employee> login(Employee employee) {

        log.info("用户输入的账号密码：{}", JSON.toJSONString(employee));

        //1. 根据用户查找是否存在该用户
        Employee employeeDb = employeeDao.findByName(employee);
        log.info("数据库的账号密码：{}", JSON.toJSONString(employeeDb));

        //2. 判断用户名是否查找到用户，如果没有查找到直接失败
        if(null==employeeDb){
            return R.error("用户名不存在");
        }
        //3. 对比密码是否正确   用户输入的密码是明文的，需要进行md5加密才能够对比
        String inputPassword = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        if(!employeeDb.getPassword().equals(inputPassword)){
            return R.error("密码不正确");
        }
        //4. 查看登录用户的状态是否正常
        if(employeeDb.getStatus()==0){
            return R.error("用户被禁用");
        }

        //5. 如果上面三个情况都符合，允许登录
          return R.success(employeeDb);
    }

    @Override
    public void save(Employee employee) {
        Employee employeeDb = employeeDao.findByName(employee);
        if(null!=employeeDb){
            throw new NameExistsException(employeeDb.getUsername());
        }

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(1);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        log.info("employee:{}",JSON.toJSONString(employee));
        employeeDao.save(employee);
    }

    @Override
    public Page<Employee> findPage(Integer page, Integer pageSize,String name) {
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);

        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        pageInfo =   employeeDao.selectPage(pageInfo, queryWrapper);
        return pageInfo;
    }

    @Override
    public void updateEmp(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Employee> updateWrapper=new LambdaUpdateWrapper<>();
        if(employee.getUsername()!=null && employee.getUsername()!=""){
            updateWrapper.set(Employee::getUsername,employee.getUsername());
        }
        if(employee.getName()!=null && employee.getName()!=""){
            updateWrapper.set(Employee::getName,employee.getName());
        }
        if(employee.getPassword()!=null && employee.getPassword()!=""){
            updateWrapper.set(Employee::getPassword,employee.getPassword());
        }
        if(employee.getPhone()!=null && employee.getPhone()!=""){
            updateWrapper.set(Employee::getPhone,employee.getPhone());
        }
        if(employee.getSex()!=null && employee.getSex()!=""){
            updateWrapper.set(Employee::getSex,employee.getSex());
        }
        if(employee.getIdNumber()!=null && employee.getIdNumber()!=""){
            updateWrapper.set(Employee::getIdNumber,employee.getIdNumber());
        }
        if(employee.getStatus()!=null){
            updateWrapper.set(Employee::getStatus,employee.getStatus());
        }
        if(employee.getUpdateTime()!=null ){
            updateWrapper.set(Employee::getUpdateTime,employee.getUpdateTime());
        }
        if(employee.getUpdateUser()!=null ){
            updateWrapper.set(Employee::getUpdateUser,employee.getUpdateUser());
        }
        updateWrapper.eq(Employee::getId,employee.getId());
        employeeDao.update(employee,updateWrapper);
    }

    /**
     * 根据id查找员工
     * @param id
     * @return
     */
    @Override
    public Employee findById(Long id) {
        Employee employee = employeeDao.selectById(id);
        return employee;
    }

    @Override
    public List<Employee> findAll() {
        return employeeDao.selectList(null);
    }
}
